package com.teacherprofile.common.converters.technical_services;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.teacherprofile.common.constant.CacheConstants;
import com.teacherprofile.common.core.domain.entity.SysDictData;

import java.util.List;
import java.util.Objects;

import static com.teacherprofile.common.utils.DictUtils.getDictCache;

/**
 * @author linli
 */

/**
 * 软件获取方式转换器
 */
public class TechnicalServicesConverter implements Converter<Integer> {
    /**
     * 获取redis缓存key
     */
    private static final String TECHNICAL_SERVICES = CacheConstants.GetTechnicalServicesKey();

    @Override
    public Class supportJavaTypeKey() {
        return Integer.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Integer convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        String value = "";
        String str = cellData.getStringValue();
        List<SysDictData> sysDictDataList = getDictCache(TECHNICAL_SERVICES);
        if (sysDictDataList.size() != 0) {
            SysDictData sysDictData = sysDictDataList.stream().filter(s -> Objects.equals(s.getDictLabel(), str)).findFirst().orElse(null);
            if (sysDictData != null) {
                value = sysDictData.getDictValue();
            }
        }
        return Integer.parseInt(value);
    }


//    @Override
//    public Integer convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
//        // 1. 获取单元格值并处理 null
//        String cellValue = cellData.getStringValue();
//        if (cellValue == null || cellValue.trim().isEmpty()) {
//            throw new RuntimeException("单元格值为空，无法转换为整数");
//        }
//
//        // 2. 从缓存获取字典数据
//        List<SysDictData> sysDictDataList = getDictCache(TYPE_METHOD);
//        if (sysDictDataList == null || sysDictDataList.isEmpty()) {
//            throw new RuntimeException("字典数据为空，无法进行映射：" + TYPE_METHOD);
//        }
//
//        // 3. 查找匹配的字典项
//        String cellValueTrimmed = cellValue.trim();
//        SysDictData matchedDictData = sysDictDataList.stream()
//                .filter(dict -> Objects.equals(dict.getDictLabel(), cellValueTrimmed))
//                .findFirst()
//                .orElse(null);
//
//        // 4. 检查是否找到匹配项
//        if (matchedDictData == null) {
//            throw new RuntimeException("字典中未找到匹配项：" + cellValueTrimmed);
//        }
//
//        // 5. 安全转换为整数
//        try {
//            return Integer.parseInt(matchedDictData.getDictValue());
//        } catch (NumberFormatException e) {
//            throw new RuntimeException("字典值格式错误，无法转换为整数：" + matchedDictData.getDictValue(), e);
//        }
//    }

    @Override
    public WriteCellData convertToExcelData(Integer value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        String str = "未知";
        //        Redis
        List<SysDictData> sysDictDataList = getDictCache(TECHNICAL_SERVICES);
        if (sysDictDataList.size() != 0) {
            SysDictData sysDictData = sysDictDataList.stream().filter(s -> Objects.equals(s.getDictValue(), value.toString())).findFirst().orElse(null);
            if (sysDictData != null) {
                str = sysDictData.getDictLabel();
            }
        }
        return new WriteCellData(str);
    }
}
